package cn.edu.neusoft.practice.Controller;

import io.swagger.annotations.Api;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.neusoft.practice.mapper.DoctorWorkMapper;
import cn.edu.neusoft.practice.mapper.WorkPlanMapper;
import cn.edu.neusoft.practice.po.DoctorWork;
import cn.edu.neusoft.practice.po.DoctorWorkParameter;
import cn.edu.neusoft.practice.po.FullDoctorWork;
import cn.edu.neusoft.practice.po.WorkPlan;
import cn.edu.neusoft.practice.Service.WorkPlanService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

@CrossOrigin
@Api("swaggerDemoController相关的api")
@RestController
public class WorkPlanController {
	@Autowired
	private WorkPlanMapper workPlanMapper;
	@Autowired
	private DoctorWorkMapper doctorworkMapper;
	@Autowired
	private WorkPlanService workPlanService;
	
	/*
	 * 周一到周日的计划大纲
	 * */
	@RequestMapping("/getPlansByWeek")
	@ResponseBody
	public List<DoctorWork> getPlansByWeek(String week){
		System.out.println(week);
		List<DoctorWork> list=workPlanMapper.getWorkPlanByWeek(week);
		return list;
	}
	
	/*
	 * 周一到周日的计划大纲
	 * */
	@RequestMapping("/getPlansStatus")
	@ResponseBody
	public WorkPlan getPlansStatus(String startDay){
		Date d=this.getDate(startDay);
		WorkPlan wp=workPlanService.getPlansStatus(d);
		return wp;
	}
	
	/*
	 * 生成周一到周日的计划大纲
	 * */
	@RequestMapping("/generatePlans")
	@ResponseBody
	public int generatePlans(String startDay){
		Date d=this.getDate(startDay);
		WorkPlan wp=new WorkPlan();
		wp.setDay(d);
		wp.setStatus(0);//初始状态
		wp.setCreateBy("admin");
		wp.setWeekday("1");
		
		boolean res=workPlanService.generatePlan(wp);
		if(res)
			//return Result.ok("生成成功");
			return 1;
		else
			return -1;
			//return Result.ok("生成成功");
	}
	
	/*
	 * 修改计划大纲的状态
	 * */
	@RequestMapping("/updatePlanStatus")
	@ResponseBody
	public int updatePlanStatus(String day,int status){
		Date d=this.getDate(day);
		WorkPlan wp=new WorkPlan();
		wp.setDay(d);
		wp.setStatus(status);//初始状态
		int res=workPlanService.updateByDay(wp);
		if(res>0)
			//return Result.ok("状态修改成功");
			return 1;
		else
			//return Result.fail("状态修改失败");
			return -1;
	}
	
	
	/*
	 * 获取当前周的所有医生排班情况
	 * */
	@RequestMapping("/getDoctorWorks")
	@ResponseBody
	public List<FullDoctorWork> getDoctorWorks(String day){
		List<FullDoctorWork> list=workPlanService.getAllBaseInfo(day);
		return list;
	}
	@RequestMapping("/updateDoctorWorks")
	@ResponseBody
	public int updateDoctorWorks(String day,int departmentId,DoctorWorkParameter parameter){		
	    List<DoctorWork> doctors=parameter.getDoctorworks();
	    //System.out.println(doctors.toString());
	    Date d=this.getDate(day);
	    int year=1900+d.getYear();
	    int month=1+d.getMonth();
	    int date=d.getDate();
	    WorkPlan wp=this.workPlanMapper.selectByDay(year, month, date);
	    int res=0;
	    for(DoctorWork doctor:doctors){
	      if(doctor.getDepartId1()==departmentId)
	      {
	        
	        QueryWrapper<DoctorWork> wrapper = new QueryWrapper<DoctorWork>();
	          wrapper.eq("day", day);
	          wrapper.eq("doc_id", doctor.getDocId());
	          doctorworkMapper.delete(wrapper);
	          doctor.setDay(day);
	          doctor.setWeekday(wp.getWeekday());
	          doctor.setFeeId(doctor.getFeeId());
	          int countLeft=0;
	          if(doctor.getAmwork()!=null&&!doctor.getAmwork().equals("0"))
	            countLeft+=30;
	          if(doctor.getPmwork()!=null&&!doctor.getPmwork().equals("0"))
	            countLeft+=30;
	          doctor.setCountLeft(countLeft);//设置可挂号数量
	          doctorworkMapper.insert(doctor);
	          res++;
	      }
	    }
	    if(res>0)
	      return 1;
	    else
	      return -1;
	}
	
	/**
	 * 字符串转换成时间类型
	 * @param str
	 * @return
	 */
	public Date getDate(String str) {
	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	    Date date = null;
	    try {
	        date = dateFormat.parse(str);
	    } catch (ParseException e) {
	        e.printStackTrace();
	    }
	    return date;
	}
}
